import { fetchPost, fetchGet } from './fetch'
import { API_URL } from '../consts/apiUrl'
import { execWithCatchAsync } from './execWithCatchAsync'
import { RecordMode } from '../common/enums'

// STEP COLLECTION
export const saveStepCollectionDetail = async (detailList, { activeStepCollectionTitle, activeStepCollectionMode,
    activeStepCollectionId, allList: allDetailList }) => {
    const validDetailList = allDetailList.filter(item => item.mode !== RecordMode.delete)   // 未删除列表
    const requireBaseEnv = validDetailList.some(item => item.data && item.data.assertTypeCode === 'equalToBaseEnv' && item.code === 'assert')

    const result = await execWithCatchAsync(fetchPost, API_URL.saveStepCollectionDetail, {
        mode: activeStepCollectionMode,
        requireBaseEnv,
        id: activeStepCollectionId,
        title: activeStepCollectionTitle,
        list: validDetailList
    })

    return result
}

// SUITE
export const saveSuiteDetail = async (detailList, { activePage, activeSuiteId, activeSuiteTitle, activeSuiteMode, activeSuiteLevel, allList: allDetailList }) => {
    const validDetailList = allDetailList.filter(item => item.mode !== RecordMode.delete)   // 未删除列表
    const requireBaseEnv = validDetailList.some(item => item.data && item.data.assertTypeCode === 'equalToBaseEnv' && item.code === 'assert')

    const result = await execWithCatchAsync(fetchPost, API_URL.saveSuiteDetail, {
        mode: activeSuiteMode,
        requireBaseEnv,
        page: activePage,
        id: activeSuiteId,
        title: activeSuiteTitle,
        level: activeSuiteLevel,
        list: validDetailList
    })

    return result
}

export const deleteSuiteMaster = async ({ activeSuiteId }) => {
    const result = await execWithCatchAsync(fetchGet, API_URL.deleteSuite, { suiteId: activeSuiteId })
    return result
}

// ELEMENT
export const saveElementList = async (list, { activeElementTreeNode }) => {
    !activeElementTreeNode && (activeElementTreeNode = {})
    !activeElementTreeNode.name && (activeElementTreeNode.name = '')

    const result = await execWithCatchAsync(fetchPost, API_URL.saveElements, {
        list: list.map(item => ({ ...item, node: activeElementTreeNode.name }))
    })
    return result
}

// AJAX
export const saveAjaxList = async list => {
    const result = await execWithCatchAsync(fetchPost, API_URL.saveAjaxs, { list })
    return result
}

// MOCK
export const saveMockList = async list => {
    const result = await execWithCatchAsync(fetchPost, API_URL.saveMocks, { list })
    return result
}

// MOCK
export const saveConfig = async (configType, configValue) => {
    const result = await execWithCatchAsync(fetchPost, API_URL.saveConfig, { configType, configValue })
    return result
}

// LOG
export const clearData = async (collection) => {
    const result = await execWithCatchAsync(fetchGet, API_URL.clearData, { collection })
    return result
}

export const updateLogRemark = async (id, remark) => {
    const result = await execWithCatchAsync(fetchGet, API_URL.updateLogRemark, { id, remark })
    return result
}

// PLAN
export const addPlan = async plan => {
    const result = await execWithCatchAsync(fetchPost, API_URL.addPlan, { plan })
    return result
}